﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using Product.Domain;
using Product.Domain.DTO;
using Product.Read.API.Applcation.Command;
using Product.Read.API.Applcation.Command.Product;
using Product.Read.API.Dtos;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;

namespace Product.Read.API.Controllers
{
    /// <summary>
    /// 产品及产品分类读
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    public class ProductReadController : ControllerBase
    {
        protected readonly IMediator mediator;

        public ProductReadController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 查询所有产品分类
        /// </summary>
        /// <returns></returns>

        [HttpGet]
        public async Task<ApiResult<ProductTypeDTO>> GetAllProduct([FromQuery] ProductSelectCommand sd)
        {

            ApiResult<ProductTypeDTO> result = new ApiResult<ProductTypeDTO>();
            try
            {
                var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
                sd.User_Id = Convert.ToInt32(uid);
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                sd.Office_Id = sid;
                result.Data = await mediator.Send(sd);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 查询所有产品
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<ProductDTO>> GetAllProductInfo([FromQuery] ProductInfoSelectCommand sd)
        {
            ApiResult<ProductDTO> result = new ApiResult<ProductDTO>();
            try
            {
                var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
                sd.User_Id = Convert.ToInt32(uid);
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                sd.Office_Id = sid;
                result.Data = await mediator.Send(sd);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 反填产品
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<Products>> SetAllProductInfo([FromQuery] ProductInfoSetCommand sd)
        {
            ApiResult<Products> result = new ApiResult<Products>();
            try
            {
                result.Data = await mediator.Send(sd);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 反填产品分类
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<ProductType>> SetAllProduct([FromQuery] ProductSetCommand sd)
        {
            ApiResult<ProductType> result = new ApiResult<ProductType>();
            try
            {
                result.Data = await mediator.Send(sd);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }
        


        /// <summary>
        /// 反填产品(付洛嘉用)
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<Products>> SetAllProducts([FromQuery] ProductSetInfoCommands sd)
        {
            ApiResult<Products> result = new ApiResult<Products>();
            try
            {
                result.Data = await mediator.Send(sd);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 查询产品分类下拉列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<ProductType>>> GetProductTypeSelect()
        {
            ApiResult<List<ProductType>> result = new ApiResult<List<ProductType>>();
            try
            {
                result.Data = await mediator.Send(new ProductTypeselectInfoCommand());
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }
    }
}
